package com.bash.compta.db4o;

import com.bash.compta.data.CategorieDepense;
import com.bash.compta.data.Compte;
import com.bash.compta.data.Depense;
import com.bash.compta.data.Login;
import com.bash.jcouture.util.swing.MySwingUtilities;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.config.ConfigScope;
import com.db4o.config.EmbeddedConfiguration;
import com.db4o.constraints.UniqueFieldValueConstraint;
import com.db4o.constraints.UniqueFieldValueConstraintViolationException;
import com.db4o.ext.DatabaseFileLockedException;
import com.db4o.ext.Db4oIOException;

public class DBTool {

    public static ObjectContainer db;
    private static DBTool instance = null;

    private DBTool() {

        System.out.println("Initialisation de la base des données...");
        EmbeddedConfiguration configuration = Db4oEmbedded.newConfiguration();

        configuration.file().generateUUIDs(ConfigScope.GLOBALLY);
        configuration.file().generateCommitTimestamps(true);

        configuration.common().objectClass(Compte.class).objectField("littera").indexed(true);
        configuration.common().add(new UniqueFieldValueConstraint(Compte.class, "littera"));

        configuration.common().objectClass(Depense.class).objectField("compte").indexed(true);
        configuration.common().add(new UniqueFieldValueConstraint(Depense.class, "compte"));

        configuration.common().objectClass(CategorieDepense.class).objectField("libele").indexed(true);
        configuration.common().add(new UniqueFieldValueConstraint(CategorieDepense.class, "libele"));

        configuration.common().objectClass(Login.class).objectField("username").indexed(true);
        configuration.common().add(new UniqueFieldValueConstraint(Login.class, "username"));


        try {
            System.out.println("Ouverture de la base des données...");
            db = Db4oEmbedded.openFile(configuration, "compta.db");


        } catch (DatabaseFileLockedException e) {
            MySwingUtilities.showMessageDialog("La base des données db4o est déjà ouverte par"
                    + " une autre instance de \n l'application , le programme va s'arrêter!");
           
            System.exit(0);

        } catch (Db4oIOException e) {
            MySwingUtilities.showMessageDialog("Problème d'accès à la base des données db4o!");
            e.printStackTrace();
            System.exit(0);
        }
    }

    public static DBTool getInstance() {

        if (instance != null) {
            return instance;
        } else {

            instance = new DBTool();
        }
        return instance;
    }

    public void store(Object o) {
        db.store(o);

        try {
             db.commit();
        } catch (UniqueFieldValueConstraintViolationException e) {
            MySwingUtilities.showErrorMessageDialog("Tentative de violation d'une valeur marquée unique !");
            db.rollback();
        }

    }

    public void close() {
        try {
        } finally {
            db.close();
        }
    }
}
